package mapData;

import gui.GamePanel;

import java.util.ArrayList;
import java.util.Collections;

public class CoordCluster extends Coord
{
	private int level;
	
	//store bounds
	private Coord topLeft, bottomRight;
	private ArrayList<Coord> localTransitions = new ArrayList<Coord>(0);
	
	
	
    
    public CoordCluster(int i, int j, Coord topLeft2, Coord bottomRight2, int level2) {
		x = i;
		y = j;
		topLeft = topLeft2;
		bottomRight = bottomRight2;
		level = level2;
	}
    
    public Coord getTopLeft()
    {
    	return this.topLeft;
    }
    
    public Coord getBottomRight()
    {
    	return this.bottomRight;
    }

	public String toString()
    {
        String xStr = x + "";
        String yStr = y + "";
        return new String("Level " + level+ " Coord Cluster at ("+ xStr +","+yStr+")");
    }
    
    public boolean isValid() //overridden to prevent erroneous results, though you should probably never use this
    {
    	if(this.x >= 0 && this.y >=0 && this.x < GamePanel.aMap.getWidth() && this.y < GamePanel.aMap.getHeight())
    	{
    		return true;
    	}
    	return false;
    }
    
    /**
     * Checks if a certain Coord is contained in this cluster
     * @param searchedFor
     * @return true if searchedFor is contained within this cluster
     */
    public boolean isContained(Coord searchedFor)
    {
    	if(searchedFor.x>=topLeft.x && searchedFor.y>=topLeft.y && x<=bottomRight.x && y <= bottomRight.y)
            return true;
        else
            return false;
    	
    }
    
    
    /**
     * deprecated function, should not be used from this subclass
     */
    public ArrayList<Coord> getUnobstructedNeighbors()
    {
    	
        return null;
    }
    
    public ArrayList<Coord> getEmptyNeighbors()
    // returns up to 4 unoccupied and not completely obstructed neighbors in 4 cardinal directions
	{
    	Coord xy = this;
	    int x = xy.getX();
	    int y = xy.getY();
	    ArrayList<Coord> nbrs = new ArrayList<Coord>(0);
	    CoordCluster n = GamePanel.aMap.getCluster(x, y-1);
	    if(n != null && !GamePanel.pfinder.isOnObstruction(n)) nbrs.add(n);
	    CoordCluster e = GamePanel.aMap.getCluster(x+1, y);
	    if(e != null && !GamePanel.pfinder.isOnObstruction(e)) nbrs.add(e);
	    CoordCluster w = GamePanel.aMap.getCluster(x-1, y);
	    if(w != null && !GamePanel.pfinder.isOnObstruction(w)) nbrs.add(w);
	    CoordCluster s = GamePanel.aMap.getCluster(x, y+1);
	    if(s != null && !GamePanel.pfinder.isOnObstruction(s)) nbrs.add(s);
	    	    
	    return nbrs;
	    	    
	}
    
    public void addLocalTransitionNode(TransitionNode t)
    {
    	Collections.sort(localTransitions);
    	if(Collections.binarySearch(localTransitions, t)<0) //if t not already added
    	{
    		localTransitions.add(t);
    		connectCoordToLocalTransitions(t);
    		
    	}
    }
    
    /**
     * creates a Transition Node with the coordinates of c 
     * and connects it to local Transition Nodes
     * @param c
     * @returns true if connected to at least 1 transition
     */
    public boolean connectCoordToLocalTransitions(Coord c)  
    {
    	return true;
    }
    
    
    
    

}
